package com.test.ad;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.Map;

import io.flutter.plugin.platform.PlatformView;

/**
 * 茶叶广告视图
 */
public class InsertAdView implements PlatformView {
    private View view;

    public InsertAdView(Context context, int id, Object args) {
        String index = "";
        String adUnitId = "";
        if (args instanceof Map) {
            // 获取传递的参数
            Map<String, Object> map = (Map<String, Object>) args;
            index = "" + map.get("pos");
            adUnitId = "" + map.get("adUnitId");
        }
        // 创建 LinearLayout
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setGravity(Gravity.CENTER);

        // 创建 TextView
        TextView textView = new TextView(context);
        textView.setId(View.generateViewId());
        textView.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        textView.setText("我是索引" + index + "茶叶广告哦! id: " + id);
        textView.setTextSize(20);

        // 将 TextView 添加到 LinearLayout 中
        linearLayout.addView(textView);

        // 设置 view
        this.view = linearLayout;
        String finalAdUnitId = adUnitId;
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击事件
                Toast.makeText(context, "广告id: " + finalAdUnitId, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Nullable
    @Override
    public View getView() {
        return view;
    }

    @Override
    public void dispose() {

    }
}
